package cn.ly.design.structure;

/**
 * 桥接模式就是把事物和其具体实现分开，使他们可以各自独立的变化。
 * 桥接的用意是：将抽象化与实现化解耦，使得二者可以独立变化。
 * 
 * 通过对Bridge类的调用，实现了对接口Sourceable的实现类SourceSub1和SourceSub2的调用。
 * @author liuyu
 *
 */

public class MyBridge extends Bridge{
	
	public static void main(String[] args) {

		Bridge b = new MyBridge();
		
		//使用BSourceSub1
		BSourceable s1 = new BSourceSub1();
		b.setBSource(s1);
		b.method();
		
		//使用BSourceSub2
		BSourceable s2 = new BSourceSub2();
		b.setBSource(s2);
		b.method();
	}
}

abstract class Bridge{
	private BSourceable bsource;
	
	public void method(){
		this.bsource.method();
	}
	
	public void setBSource(BSourceable bsource){
		this.bsource = bsource;
	}
	
	
}


interface BSourceable {  
	void method();  
}  

class BSourceSub1 implements BSourceable {  
  
    @Override  
    public void method() {  
        System.out.println("this is the first sub!");  
    }  
}  

class BSourceSub2 implements BSourceable {  
  
    @Override  
    public void method() {  
        System.out.println("this is the second sub!");  
    }  
}  

